using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Budgetor
{
    public class BudgetFile
    {
        private string path;
        private List<Data> dataCollection = new List<Data>();

        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        private DateTime allowanceFrom;

        public DateTime AllowanceFrom
        {
            get { return allowanceFrom; }
            set { allowanceFrom = value; }
        }

        private DateTime allowanceTo;

        public DateTime AllowanceTo
        {
            get { return allowanceTo; }
            set { allowanceTo = value; }
        }

        private bool useCurrentDateForAllowance;

        public bool UseCurrentDateForAllowance
        {
            get { return useCurrentDateForAllowance; }
            set { useCurrentDateForAllowance = value; }
        }

        private float exchangeRate;

        public float ExchangeRate
        {
            get { return exchangeRate; }
            set { exchangeRate = value; }
        }

        private float dailyAllowance;

        public float DailyAllowance
        {
            get { return dailyAllowance; }
            set { dailyAllowance = value; }
        }

        public List<Data> DataCollection
        {
            get { return dataCollection; }
            set { dataCollection = value; }
        }

        public static BudgetFile Load(string path)
        {
            BudgetFile budgetFile;

            if (File.Exists(path))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(BudgetFile));
                using (StreamReader reader = new StreamReader(path))
                {
                    budgetFile = serializer.Deserialize(reader) as BudgetFile;
                }
            }
            else
                budgetFile = new BudgetFile();

            budgetFile.path = path;
            return budgetFile;
        }

        public void Save()
        {
            // Sort the data before saving
            dataCollection.Sort();

            if (path == string.Empty)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Budget files (*.bdf)|*.bdf";

                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    path = saveFileDialog.FileName;
                else
                    return;
            }
            
            // Now save it to an XML file
            XmlSerializer serializer = new XmlSerializer(typeof(BudgetFile));
            using (StreamWriter writer = new StreamWriter(path))
            {
                serializer.Serialize(writer, this);
            }
        }

        public void SaveAs()
        {
            string currentPath = path;
            path = string.Empty;
            Save();
            path = currentPath;
        }
    }
}
